import 'package:flutter/material.dart';

import '../services/screen_adapter.dart';
import '../services/search_service.dart';

class SearchPage extends StatefulWidget {
  SearchPage({Key key}) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {

  var _keywords;
  List _historyListData = [];

  @override
  void initState() { 
    super.initState();
    this._getHistoryData();
  }

  _getHistoryData() async{
    var historyListData = await SearchService.getHistoryListData();
    setState(() => this._historyListData = historyListData);
  }

  _showAlertDialog(keywords) async{
    var result = await showDialog(
      context: context,
      barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
      builder: (context) {
        return AlertDialog(
          title: Text('提示信息'),
          content: Text('您确定要删除吗?'),
          actions: <Widget>[
            FlatButton(
              onPressed: () {
                Navigator.pop(context, 'Cancle');
              }, 
              child: Text('取消')
            ),
            FlatButton(
              onPressed: () async{
                await SearchService.removeHistoryData(keywords);
                this._getHistoryData();
                Navigator.pop(context, 'Ok');
              }, 
              child: Text('确定')
            )
          ],
        );
      }
    );
    print(result);
  }

  Widget _historyListWidget() {
    if(_historyListData.length > 0) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
          child: Text('历史记录', 
            style: Theme.of(context).textTheme.headline6) //使用主题AppBar.title一样的样式
          ),
          Divider(),
          Column(
            children: this._historyListData.map((e) {
              return Column(
                children: <Widget>[
                  ListTile(
                    title: Text('$e'),
                    onLongPress: (){ //长按
                      this._showAlertDialog(e);
                    },
                  ),
                  Divider()
                ],
              );
            }).toList(),
          ),
          SizedBox(height: 100),
          InkWell(
            onTap: () {
              SearchService.clearHistoryList();
              this._getHistoryData();
            },
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: ScreenAdapter.width(400),
                  height: ScreenAdapter.width(64),
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Color.fromRGBO(233, 233, 233, 1),
                      width: 1
                    )
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Icon(Icons.delete),
                      Text('清空历史记录', textAlign: TextAlign.center,)
                    ]
                  )
                )  
              ]
            )
          )
        ],
      );
    } else {
      return Text('');
    }
    
  }


  @override
  Widget build(BuildContext context) {

    ScreenAdapter.init();

    return Scaffold(
      appBar: AppBar(
        title: Container(
          height: ScreenAdapter.height(68), 
          decoration: BoxDecoration(
              color: Color.fromRGBO(233, 233, 233, 0.8),
              borderRadius: BorderRadius.circular(30)
            ),
          child: TextField(
            autofocus: true,
            decoration: InputDecoration(
              border: OutlineInputBorder(
                borderSide: BorderSide.none, //去掉四周的边框
                borderRadius: BorderRadius.circular(30)
              )
            ),
            onChanged: (value) {
              setState(() => this._keywords = value);
            }
          )
        ),
        actions: <Widget>[
          InkWell(
            onTap: () {
              // 存储关键字
              SearchService.setHistoryListData(this._keywords);
              Navigator.pushReplacementNamed(context, '/productList', arguments: {
                'keywords': this._keywords
              });
            }, 
            child: Container(
              height: ScreenAdapter.height(68), 
              width: ScreenAdapter.height(80),
              child: Row(
                // mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('搜索')
                ],
              )
            )
          )
        ],
      ), 
      body: Container(
        padding: EdgeInsets.all(10),
        child: ListView(
          children: <Widget>[
            Container(
              child: Text('热搜', style: Theme.of(context).textTheme.headline6) //使用主题AppBar.title一样的样式
            ),
            Divider(),
            Wrap(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(233, 233, 233, 0.9),
                    borderRadius: BorderRadius.circular(10)
                  ),
                  child: Text('女装') 
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(233, 233, 233, 0.9),
                    borderRadius: BorderRadius.circular(10)
                  ),
                  child: Text('笔记本电脑') 
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(233, 233, 233, 0.9),
                    borderRadius: BorderRadius.circular(10)
                  ),
                  child: Text('水冷机箱') 
                ),
                Container(
                  padding: EdgeInsets.all(10),
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(233, 233, 233, 0.9),
                    borderRadius: BorderRadius.circular(10)
                  ),
                  child: Text('苹果') 
                )
              ],
            ),
            SizedBox(height: 10),
            // 历史记录
            _historyListWidget()
          ], 
        )
      )
    );
  }
}